package com.coollang.smartbra.presenter2.impl;

import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.interfaces.view.LoginView;
import com.coollang.smartbra.model.BaseModel;
import com.coollang.smartbra.model.LogInDataBean;
import com.coollang.smartbra.model.LoginModel;
import com.coollang.smartbra.model.UserModel;
import com.coollang.smartbra.presenter2.LoinPresenter;
import com.coollang.smartbra.utils.Urls;

import java.util.HashMap;
import java.util.Map;

import io.realm.Realm;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @Auther: Timmy
 * @Data: 2017/7/28
 * @Fuction:
 */

public class LoinPresenterImpl extends BasePresenterImpl implements LoinPresenter {

    private LoginView mLoginView;

    public LoinPresenterImpl(LoginView loginView) {
        mLoginView = loginView;
    }

    @Override
    public void login(final String username, final String password) {

        Map<String, String> map = new HashMap<>();
        map.put(Urls.USERNAME, username);
        map.put(Urls.PASSWORD, password);

        MyHttp.getInstance().login(map, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                LoginModel loginModel = mGson.fromJson(results, LoginModel.class);
                int code = Integer.parseInt(loginModel.getCode());
                if (200 == code) {
                    savaUserData(username, password);
                    saveLoginData(loginModel.getData());
                } else {
                    mLoginView.onLoginFailed(code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mLoginView.onLoginFailed(Urls.CODE_Error_);
            }
        });
    }

    private void saveLoginData(final LogInDataBean data) {
        MyApplication.mLogInDataBean = data;
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                //保证存储的数据唯一
                data.setUid(1);
                mRealm.copyToRealmOrUpdate(data);

                // 登录成功
                if (data.getIsInit() == 0) {
                    mLoginView.goToWelcomeAct();
                } else {
                    mLoginView.goToMainAct();
                }
            }
        });
    }

    @Override
    public void checkUserIsRegister(String username) {
        MyHttp.getInstance().checkUserIsRegister(username, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                BaseModel model = mGson.fromJson(results, BaseModel.class);
                int code = Integer.parseInt(model.getCode());
                if (200 == code) {
                    // 获取成功
                    mLoginView.onIsRegister(model.getData());
                } else {
                    mLoginView.onLoginFailed(code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mLoginView.onLoginFailed(Urls.CODE_Error_);
            }
        });
    }


    // 获取验证码
    @Override
    public void getCode(String username) {
        MyHttp.getInstance().getCode(username, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                LoginModel loginModel = mGson.fromJson(results, LoginModel.class);
                int code = Integer.parseInt(loginModel.getCode());
                if (Urls.CODE_SUCCESS == code) {
                    // 获取成功
                    mLoginView.onSucceed();
                } else {
                    mLoginView.onLoginFailed(code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mLoginView.onLoginFailed(Urls.CODE_Error_);
            }
        });
    }

    // 注册
    @Override
    public void register(final String username, final String password, String code) {

        Map<String, String> map = new HashMap<>();
        map.put(Urls.USERNAME, username);
        map.put(Urls.PASSWORD, password);
        map.put(Urls.VALIDATECODE, code);
        map.put(Urls.ZONE, "cn");
        map.put(Urls.CODE, "code");
        map.put(Urls.DEVICE, "1");

        MyHttp.getInstance().register(map, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                LoginModel loginModel = mGson.fromJson(results, LoginModel.class);
                int code = Integer.parseInt(loginModel.getCode());
                if (code == Urls.CODE_SUCCESS) {
                    savaUserData(username, password);
                    saveLoginData(loginModel.getData());
                } else {
                    mLoginView.onLoginFailed(code);
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                mLoginView.onLoginFailed(Urls.CODE_Error_);
            }
        });
    }

    @Override
    public void changePassword(String username, String password, String code) {

    }


    private void savaUserData(final String username, final String password) {
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                UserModel model = new UserModel();

                model.setUsername(username);
                model.setPassword(password);

                realm.copyToRealmOrUpdate(model);
            }
        });
    }

}
